import Form from "../form.vue";
import { handleTree } from "@/utils/tree";
import { menuApi } from "@/api/system/menu";
import { transformI18n } from "@/plugins/i18n";
import { reactive, h, Ref, shallowRef } from "vue";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { cloneDeep, isAllEmpty, isFunction } from "@pureadmin/utils";
import { hasAuth } from "@/router/utils";

export function useMenu(tableRef: Ref) {
  const api = reactive({
    list: menuApi.list,
    fields: menuApi.fields,
    save: menuApi.save,
    delete: menuApi.delete
  });
  const auth = reactive({
    list: true,
    create: hasAuth("add"),
    update: hasAuth("edit"),
    delete: hasAuth("delete"),
    batchDelete: hasAuth("delete"),
    import: hasAuth("import"),
    export: hasAuth("export"),
    download: hasAuth("download"),
    auth: hasAuth("auth")
  });
  const getMenuType = (type, text = false) => {
    switch (type) {
      case 0:
        return text ? "菜单" : "primary";
      case 1:
        return text ? "iframe" : "warning";
      case 2:
        return text ? "外链" : "danger";
      case 3:
        return text ? "按钮" : "info";
    }
  };
  const columns: TableColumnList = [
    {
      label: "菜单名称",
      prop: "title",
      align: "left",
      cellRenderer: ({ row }) => (
        <>
          <span class="inline-block mr-1">
            {h(useRenderIcon(row.icon), {
              style: { paddingTop: "1px" }
            })}
          </span>
          <span>{transformI18n(row.title)}</span>
        </>
      )
    },
    {
      label: "菜单类型",
      prop: "menuType",
      width: 100,
      cellRenderer: ({ row, props }) => (
        <el-tag
          size={props.size}
          type={getMenuType(row.menuType)}
          effect="plain"
        >
          {getMenuType(row.menuType, true)}
        </el-tag>
      )
    },
    {
      label: "路由路径",
      prop: "path"
    },
    {
      label: "组件路径",
      prop: "component",
      formatter: ({ path, component }) =>
        isAllEmpty(component) ? path : component
    },
    {
      label: "权限标识",
      prop: "auth_code"
    },
    {
      label: "排序",
      prop: "rank",
      width: 100
    },
    {
      label: "隐藏",
      prop: "showLink",
      formatter: ({ showLink }) => (showLink ? "否" : "是"),
      width: 100
    },
    {
      label: "操作",
      fixed: "right",
      width: 210,
      slot: "operation"
    }
  ];
  function formatHigherMenuOptions(treeList) {
    if (!treeList || !treeList.length) return;
    const newTreeList = [];
    for (let i = 0; i < treeList.length; i++) {
      treeList[i].title = transformI18n(treeList[i].title);
      formatHigherMenuOptions(treeList[i].children);
      newTreeList.push(treeList[i]);
    }
    return newTreeList;
  }
  const editForm = shallowRef({
    form: Form,
    props: {
      formInline: row => {
        return {
          menuType: row?.menuType ?? 0,
          higherMenuOptions: formatHigherMenuOptions(
            cloneDeep(tableRef.value.dataList)
          ),
          parentId: row?.parentId ?? 0,
          title: row?.title ?? "",
          name: row?.name ?? "",
          path: row?.path ?? "",
          component: row?.component ?? "",
          rank: row?.rank ?? 99,
          redirect: row?.redirect ?? "",
          icon: row?.icon ?? "",
          extraIcon: row?.extraIcon ?? "",
          enterTransition: row?.enterTransition ?? "",
          leaveTransition: row?.leaveTransition ?? "",
          activePath: row?.activePath ?? "",
          auth_code: row?.auth_code ?? "",
          frameSrc: row?.frameSrc ?? "",
          frameLoading: row?.frameLoading ?? true,
          keepAlive: row?.keepAlive ?? false,
          hiddenTag: row?.hiddenTag ?? false,
          fixedTag: row?.fixedTag ?? false,
          showLink: row?.showLink ?? true,
          showParent: row?.showParent ?? false,
          id: row?.id ?? 0
        };
      }
    }
  });
  function resultFormat(data) {
    return handleTree(data);
  }
  function openDialog(row) {
    if (tableRef.value && isFunction(tableRef.value.openDialog)) {
      tableRef.value.openDialog(true, { parentId: row.id });
    } else {
      console.error("openDialog method is not defined on the child component");
    }
  }
  return { api, auth, columns, resultFormat, editForm, openDialog };
}
